﻿Imports NMock2

Imports System.Collections.Generic
Imports Microsoft.VisualStudio.TestTools.UnitTesting

Imports Meiners.Libs.Withings
Imports Meiners.Libs.Withings.Core



'''<summary>
'''This is a test class for WithingsAccountTest and is intended
'''to contain all WithingsAccountTest Unit Tests
'''</summary>
<TestClass()> _
Public Class AccountTest


    Private testContextInstance As TestContext

    '''<summary>
    '''Gets or sets the test context which provides
    '''information about and functionality for the current test run.
    '''</summary>
    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(ByVal value As TestContext)
            testContextInstance = value
        End Set
    End Property

    '''<summary>
    '''A test for ComputerHash
    '''</summary>
    <TestMethod()> _
    Public Sub ComputerHashTest()

        Dim target As Account_Accessor
        Dim mocks As NMock2.Mockery = New NMock2.Mockery()
        Dim webMock As IHttpConnector = Nothing

        webMock = CType(mocks.NewMock(GetType(IHttpConnector)), IHttpConnector)

        Stub.On(webMock).Method("GetWebserviceResponse").Will([Return].Value(My.Resources.getMagicString_SampleResult))
        WBSAPIConnector.WebConnector = webMock


        target = New Account_Accessor("demo@withings.com", "bozo")
        Dim expected As String = "99eb3bc8555ca782fa4a0fda53bc8a1a"
        Dim actual As String
        actual = target.ComputeHash()

        Assert.AreEqual(Of String)(expected, actual)
    End Sub
    '''<summary>
    '''A test for ComputerHash
    '''</summary>
    <TestMethod()> _
    Public Sub ComputerHashTest_fail()

        Dim target As Account_Accessor
        Dim mocks As NMock2.Mockery = New NMock2.Mockery()
        Dim webMock As IHttpConnector = Nothing

        webMock = CType(mocks.NewMock(GetType(IHttpConnector)), IHttpConnector)
        WBSAPIConnector.WebConnector = Nothing
        Stub.On(webMock).Method("GetWebserviceResponse").Will([Return].Value(My.Resources.getMagicString_SampleResult))
        WBSAPIConnector.WebConnector = webMock

        target = New Account_Accessor("demo@withings.com", "bozo")
        Dim expected As String = "99eb3bc8555ca782fa4a0fda53bc8a1a-fail"
        Dim actual As String
        actual = target.ComputeHash()

        Assert.AreNotEqual(Of String)(expected, actual)
    End Sub



    '''<summary>
    '''A test for WithingsAccount Constructor
    '''</summary>
    <TestMethod()> _
    Public Sub AccountConstructorTest()
        Dim emailAddress As String = Guid.NewGuid.ToString
        Dim password As String = Guid.NewGuid.ToString
        Dim mocks As NMock2.Mockery = New NMock2.Mockery()
        Dim webMock As IHttpConnector = Nothing

        webMock = CType(mocks.NewMock(GetType(IHttpConnector)), IHttpConnector)
        WBSAPIConnector.WebConnector = Nothing
        Stub.On(webMock).Method("GetWebserviceResponse").Will([Return].Value(My.Resources.getMagicString_SampleResult))
        WBSAPIConnector.WebConnector = webMock

        Dim target As Account = New Account(emailAddress, password)
        Assert.IsInstanceOfType(target, GetType(Account))
    End Sub
    '''<summary>
    '''A test for WithingsAccount Constructor
    '''</summary>
    <TestMethod(), _
    ExpectedException(GetType(System.ArgumentNullException))> _
    Public Sub AccountConstructorTest_Para1_Test()
        Dim emailAddress As String = Nothing
        Dim password As String = Guid.NewGuid.ToString
        Dim target As Account = New Account(emailAddress, password)
    End Sub
    '''<summary>
    '''A test for WithingsAccount Constructor
    '''</summary>
    <TestMethod(), _
    ExpectedException(GetType(System.ArgumentNullException))> _
    Public Sub AccountConstructorTest_Para2_Test()
        Dim emailAddress As String = Guid.NewGuid.ToString
        Dim password As String = Nothing
        Dim target As Account = New Account(emailAddress, password)
    End Sub


    '''<summary>
    '''A test for GetWithingsUsers
    '''</summary>
    <TestMethod()> _
    Public Sub GetUserListTest_Success()
        Dim emailAddress As String = "demo@withings.com"
        Dim password As String = "bozo"
        Dim actual As GetUserListResponse
        Dim target As Account

        Dim webMock As IHttpConnector = Nothing
        Dim mocks As NMock2.Mockery


        mocks = New NMock2.Mockery()
        webMock = CType(mocks.NewMock(GetType(IHttpConnector)), IHttpConnector)
        Stub.On(webMock).Method("GetWebserviceResponse").Will([Return].Value(My.Resources.getMagicString_SampleResult))
        WBSAPIConnector.WebConnector = webMock

        target = New Account(emailAddress, password) ' TODO: Initialize to an appropriate value
        webMock = Nothing
        mocks = Nothing


        mocks = New NMock2.Mockery()
        webMock = CType(mocks.NewMock(GetType(IHttpConnector)), IHttpConnector)
        Stub.On(webMock).Method("GetWebserviceResponse").Will([Return].Value(My.Resources.getUserList_SampleResult))
        WBSAPIConnector.WebConnector = webMock

        actual = target.GetUserList

        Assert.IsTrue((From x As User In actual.Users _
                       Where x.PublicKey = "3753a06f1c5ee6a0").Count = 1, "SampleUser John Doe not found")
        Assert.IsTrue((From x As User In actual.Users _
                       Where x.PublicKey = "3e203cb659c03995").Count = 1, "SampleUser Jane Doe not found")
        Assert.IsTrue((From x As User In actual.Users _
                       Where x.PublicKey = "051fc9bb6fa571a9").Count = 1, "SampleUser Natalie Doe not found")

    End Sub

    '''<summary>
    '''A test for GetWithingsUsers
    '''</summary>
    <TestMethod()> _
    Public Sub GetUserListTest_Fail()
        Dim emailAddress As String = "demo@withings.com"
        Dim password As String = "bozo"
        Dim target As Account = New Account(emailAddress, password) ' TODO: Initialize to an appropriate value
        Dim actual As GetUserListResponse

        Dim mocks As NMock2.Mockery = New NMock2.Mockery()
        Dim webMock As IHttpConnector = Nothing

        webMock = CType(mocks.NewMock(GetType(IHttpConnector)), IHttpConnector)

        Stub.On(webMock).Method("GetWebserviceResponse").Will([Return].Value(My.Resources.Error_2555_SampleResult))
        WBSAPIConnector.WebConnector = webMock

        actual = target.GetUserList
        Assert.IsInstanceOfType(actual, GetType(GetUserListResponse))
        Assert.AreEqual(Of StatusResponseEnum)(StatusResponseEnum.AnUnknownErrorOccured, actual.Status, "Status Property is not correct")
    End Sub


    '''<summary>
    '''A test for GetUserByID
    '''</summary>
    <TestMethod(), _
    ExpectedException(GetType(System.ArgumentNullException))> _
    Public Sub GetUserByIDTest_Fail_Parameter_1()
        Dim emailAddress As String = "demo@withings.com"
        Dim password As String = "bozo"
        Dim userID As Integer = -1
        Dim actual As User
        Dim target As Account

        Dim webMock As IHttpConnector = Nothing
        Dim mocks As NMock2.Mockery


        mocks = New NMock2.Mockery()
        webMock = CType(mocks.NewMock(GetType(IHttpConnector)), IHttpConnector)
        Stub.On(webMock).Method("GetWebserviceResponse").With(New String() {"http://wbsapi.withings.net/once?action=get"}).Will([Return].Value(My.Resources.getMagicString_SampleResult))
        WBSAPIConnector.WebConnector = webMock

        target = New Account(emailAddress, password)
        webMock = Nothing
        mocks = Nothing

        actual = target.GetUserById(userID)

    End Sub

    '''<summary>
    '''A test for GetUserByID
    '''</summary>
    <TestMethod()> _
    Public Sub GetUserByIDTest_Success()
        Dim emailAddress As String = "demo@withings.com"
        Dim password As String = "bozo"
        Dim userID As Integer = 29
        Dim actual As User
        Dim target As Account

        Dim webMock As IHttpConnector = Nothing
        Dim mocks As NMock2.Mockery


        mocks = New NMock2.Mockery()
        webMock = CType(mocks.NewMock(GetType(IHttpConnector)), IHttpConnector)
        Stub.On(webMock).Method("GetWebserviceResponse").With(New String() {"http://wbsapi.withings.net/once?action=get"}).Will([Return].Value(My.Resources.getMagicString_SampleResult))
        Stub.On(webMock).Method("GetWebserviceResponse").With(New String() {"http://wbsapi.withings.net/account?action=getuserslist&email=demo@withings.com&hash=99eb3bc8555ca782fa4a0fda53bc8a1a"}).Will([Return].Value(My.Resources.getUserList_SampleResult))
        WBSAPIConnector.WebConnector = webMock

        target = New Account(emailAddress, password)
        webMock = Nothing
        mocks = Nothing

        actual = target.GetUserById(userID)


        Assert.AreEqual(Of String)("3753a06f1c5ee6a0", actual.PublicKey)
        Assert.AreEqual(Of String)("John", actual.FirstName)
        Assert.AreEqual(Of String)("Doe", actual.LastName)
        Assert.AreEqual(Of String)("JON", actual.ShortName)
        Assert.AreEqual(Of GenderEnum)(GenderEnum.Male, actual.Gender)
        Assert.AreEqual(Of String)("0", actual.FatMethod)
        Assert.IsTrue(actual.IsBodyScaleDataPublic)
        Assert.AreEqual(Of Date)(New Date(1970, 1, 1, 0, 0, 0).AddSeconds(211629600), actual.Birthdate)

    End Sub
End Class
